﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Libs.Content;
using System.Data;

public partial class Pages_News_Detail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int cateID = 0, newsID = 0;
        if (!NewsUtils.CheckUrl(ref cateID, ref newsID))
        {
            Response.Redirect(Constant.HOME_PATH);
        }
        NewsCategories _Cate = new NewsCategories();
        _Cate = _Cate.Get(cateID);
        if (_Cate.CateID == 0)
        {
            Response.Redirect(Constant.HOME_PATH);
        }
        News _News = new News();
        _News = _News.Get(newsID);
        if (_News.NewsID == 0 || _News.Status != 3)
        {
            Response.Redirect(Constant.HOME_PATH);
        }

        // Kiểm tra nếu là tin ảnh
        if (cateID == 52)
        {
            Response.Redirect("/slide.html?nid=" + newsID.ToString());
        }

        lblTime.Text = _News.PublishedTime.ToString("HH'h'mm '|' dd/MM/yyyy");
        lblTitle.Text = _News.Title;
        Title = _News.Title + " - " + Constant.SiteTitle;
        ltrContent.Text = _News.Content;
        lblHits.Text = _News.Hits.ToString();

        hlCate.Text = _Cate.Name;
        hlCate.NavigateUrl = NewsUtils.CateLink(cateID.ToString(), _Cate.Url, "0");

        _News.AddHits();
        NewsWeb _NewsWeb = new NewsWeb();
        rptNewList.DataSource = _NewsWeb.GetList(_Cate.CateID, _News.NewsID, 10);
        rptNewList.DataBind();

        NewsForum _Forum = new NewsForum();
        rptCommentList.DataSource = _Forum.GetList(newsID, 2);
        rptCommentList.DataBind();
        
        // Tag
        ltrTags.Text = Tags(_News.Tags);

        System.Web.UI.HtmlControls.HtmlMeta _des = new System.Web.UI.HtmlControls.HtmlMeta();
        _des.Name = "description";
        _des.Content = _News.Lead;
        Page.Header.Controls.Add(_des);

        System.Web.UI.HtmlControls.HtmlMeta _descript = new System.Web.UI.HtmlControls.HtmlMeta();
        _descript.Name = "og:description";
        _descript.Content = _News.Lead;
        Page.Header.Controls.Add(_descript);

        System.Web.UI.HtmlControls.HtmlMeta _img = new System.Web.UI.HtmlControls.HtmlMeta();
        _img.Name = "og:image";
        _img.Content = AppUtils.ImageUrl(_News.ImageUrl, "200", "200");
        Page.Header.Controls.Add(_img);
    }

    private string Tags(string tags)
    {
        if (string.IsNullOrEmpty(tags)) return "";
        string[] list = tags.Split(',');

        string s = "";
        for (int i = 0; i < list.Length; i++)
        {
            list[i] = list[i].Trim();
            s = s + string.Format("<a href=\"{0}\">{1}</a>", Constant.HOME_PATH + "search.html?q=" + list[i], list[i]) + ", ";
        }
        return s;
    }

    protected void btComment_Click(object sender, EventArgs e)
    {
        // Mã bảo vệ
        if (Session["Code"].ToString().ToLower() != txtCaptcha.Text.Trim().ToLower())
        {
            Message.Alert(Page, "Mã bảo vệ không chính xác");
            return;
        }

        int cateID = 0, newsID = 0;
        if (!NewsUtils.CheckUrl(ref cateID, ref newsID))
        {
            Response.Redirect(Constant.HOME_PATH);
        }

        var _Forum = new NewsForum();
        _Forum.NewsID = newsID;
        _Forum.Title = "";
        _Forum.Email = AppUtils.FilterTextbox(txtEmail.Text.Trim().ToLower());
        _Forum.FullName = AppUtils.FilterTextbox(txtFullName.Text.Trim());
        _Forum.Content = AppUtils.FilterTextbox(txtContent.Text.Trim());
        _Forum.Header = _Forum.FullName;
        _Forum.Status = 1;
        _Forum.Title = _Forum.FullName;
        _Forum.Add();

        Message.Alert(Page, "Cám ơn bạn đã gửi phản hồi! Phản hồi của bạn đang được duyệt.");
        txtContent.Text = "";
        txtEmail.Text = "";
        txtFullName.Text = "";
        txtCaptcha.Text = "";
    }

}